package com.ja.openchat.ui.me;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.allen.library.SuperButton;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.views.UserAvatarView;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.group.ChatOnlineMemberCountInfo;
import com.zy.openchat.core.model.group.SuperGroupInfo;
import com.zy.openchat.core.model.message.ChatInfo;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PublicGroupAdapter extends BaseQuickAdapter<ChatInfo, BaseViewHolder> implements LoadMoreModule {

    private Map<Long,Integer> groupOnlineInfos = new HashMap<>();
    private Map<Long,SuperGroupInfo> meInGroupInfos = new HashMap<>();

    private int getOnlineNumber(final long chatId){
        if(groupOnlineInfos.containsKey(chatId)){
            return groupOnlineInfos.get(chatId);
        }
        TelegramManager.getTelegramManager().getGroupOnlineCount(getContext(), chatId, new TgResultBlock<ChatOnlineMemberCountInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, ChatOnlineMemberCountInfo obj) {
                updateOnlineInfo(chatId,obj==null?0:obj.online_member_count);

            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
        return 0;
    }

    @Override
    public void setNewInstance(@Nullable List<ChatInfo> list) {
        groupOnlineInfos.clear();
        super.setNewInstance(list);
    }

    public PublicGroupAdapter() {
        super(R.layout.item_public_group);
        addChildClickViewIds(R.id.add_btn);
    }

    public void notifyOnlineInfo(long chatId){
        groupOnlineInfos.remove(chatId);
        getOnlineNumber(chatId);
    }

    public void updateOnlineInfo(long chatId,int count){
        groupOnlineInfos.put(chatId,count);
        for(int i =0;i<getDefItemCount();i++){
            if(getItem(i).id == chatId){
                notifyItemChanged(i);
                break;
            }
        }
    }

    public void updateMeGroupStatus(SuperGroupInfo obj,boolean isFromEvent){
        if(obj == null){
            return;
        }
        meInGroupInfos.put(obj.id,obj);
        for(int i =0;i<getDefItemCount();i++){
            if(getItem(i).superGroupId() == obj.id){
                notifyOnlineInfo(getItem(i).id);
                notifyItemChanged(i);
                break;
            }
        }
    }

    public SuperGroupInfo getSuperGroupInfo(ChatInfo chatInfo){
        if(meInGroupInfos.containsKey(chatInfo.superGroupId())){
            return meInGroupInfos.get(chatInfo.superGroupId());
        }
        TelegramManager.getTelegramManager().getSuperGroupInfo(getContext(), chatInfo.superGroupId(), new TgResultBlock<SuperGroupInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, SuperGroupInfo obj) {
                updateMeGroupStatus(obj,false);
            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });
        return null;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ChatInfo chatInfo) {
        UserAvatarView user_header_image = helper.getView(R.id.user_header_image);
        user_header_image.showGroup(chatInfo);
        helper.setText(R.id.userNameText,chatInfo.title());
        SuperButton add_btn = helper.getView(R.id.add_btn);
        SuperGroupInfo superGroupInfo = getSuperGroupInfo(chatInfo);
        if(superGroupInfo!=null&&superGroupInfo.status!=null&&superGroupInfo.status.isMyGroup()){
            add_btn.setShapeSolidColor(ContextCompat.getColor(getContext(),R.color.color_f5f9fa))
                    .setShapeStrokeWidth(0)
                    .setUseShape();
            add_btn.setTextColor(ContextCompat.getColor(getContext(),R.color.me_nick_name_color_text_sub));
            add_btn.setText(getContext().getString(R.string.enter_text));
        }else{
            add_btn.setShapeSolidColor(ContextCompat.getColor(getContext(),R.color.transparent))
                    .setShapeStrokeWidth(1)
                    .setShapeStrokeColor(ContextCompat.getColor(getContext(),R.color.login_btn_bg_color))
                    .setUseShape();
            add_btn.setTextColor(ContextCompat.getColor(getContext(),R.color.login_btn_bg_color));
            add_btn.setText(getContext().getString(R.string.join_text));

        }
        int totalNumber = superGroupInfo==null?0:superGroupInfo.member_count;
        int onlineNumber = getOnlineNumber(chatInfo.id);
        helper.setText(R.id.addressInfoText, String.format(getContext().getString(R.string.total_member_online),totalNumber,onlineNumber));
    }

    @Override
    public @NotNull BaseLoadMoreModule addLoadMoreModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }
}
